﻿using System.Collections.Generic;
using System.Linq;
using Configuration;
using Model;
using Repository.Common;

namespace Repository
{
    public class DistrictRepository : Repository<District>
    {
        public DistrictRepository(Database context)
            : base(context)
        {
        }

        public District HaveProduct(int id)
        {
            GetContext().ContextOptions.LazyLoadingEnabled = true;

            var res = (from c in GetQuery().Include("Products") where c.Id == id select c).SingleOrDefault();

            return res;
        }

        public List<District> GetAllWithProvince()
        {
            GetContext().ContextOptions.LazyLoadingEnabled = true;

            var res = from c in GetQuery().Include("Province") select c;

            return res.ToList();
        }

        public District GetByIdWithProvince(int id)
        {
            GetContext().ContextOptions.LazyLoadingEnabled = true;

            var res = (from c in GetQuery().Include("Province") where c.Id == id select c).SingleOrDefault();

            return res;
        }

        public List<District> SearchByName(string name)
        {
            var res = from c in GetQuery() where c.Name.ToLower().Contains(name.ToLower()) select c;

            return res.ToList();
        }

        public List<District> Paging(int pageIndex, int numOfRows)
        {
            var skipRows = (pageIndex - 1) * numOfRows;

            return GetAllWithProvince().Skip(skipRows).Take(numOfRows).ToList();
        }

        public List<District> GetAllByProvince(int id)
        {
            GetContext().ContextOptions.LazyLoadingEnabled = true;

            var res = from c in GetQuery().Include("Province") where c.Province.Id == id select c;

            return res.ToList();
        }
    }
}